#include "urppapplet.h"

#include "playpausebutton.h"
#include "settings.h"
#include "streammanager.h"
#include "songitem.h"
#include "idledetector.h"
#include <QSettings>

#define PADDING 10

URPPApplet::URPPApplet(QObject *parent, const QVariantList &args) :
#ifdef PLASMA 
Plasma::Applet(parent, args),centerItem(0), topBar(0), animTimeLine(new QTimeLine(750)), oddItem(false) {
#else
centerItem(0), topBar(0), oddItem(false) {
	Q_UNUSED(parent);
	Q_UNUSED(args);
	setWindowTitle("Radio Paradise Player");
	setScene(new QGraphicsScene());
	scene()->setItemIndexMethod(QGraphicsScene::NoIndex);
	setWindowIcon(QPixmap(":/images/icon.png"));
	setFrameStyle(NoFrame);
	init();
#endif
	resize(750,500);
}

void URPPApplet::onUserIdle() {
	// click pause if we are playing
	if(!playPauseButton->isShowingPlay())
		playPauseButton->click();
}

void URPPApplet::onNewSong(QString artist, QString title) {	
	setWindowTitle(artist+"-"+title);
	SongItem* prevItem = centerItem;
	centerItem = new SongItem(artist, title);
	centerItem->setPos(PADDING, topBar->size().height());
	centerItem->resize(QSize(width()-2*PADDING, 0));
	scene()->addItem(centerItem);
	double centerHeight = qMin(200.0, (height()-topBar->size().height())/2);
	centerItem->animateSizeTo(QSize(width()-2*PADDING, centerHeight));
	if(prevItem) {
		itemsList.enqueue(prevItem);
		if(itemsList.size() > 10) {
			SongItem* toDelete = itemsList.dequeue();
			delete toDelete;
		}
		prevItem->useBaseColor(oddItem);
		oddItem = !oddItem;
		connect(centerItem, SIGNAL(geometryChanged(const QRectF)), prevItem, SLOT(onNextSongMoved(const QRectF)));
	}
}

void URPPApplet::showSettings() {
	Settings settings(this);
	if(settings.exec() == QDialog::Accepted) {
		QSettings settings;
		settingsLabel->setText(settings.value("stream", "128k MP3").toString()+"<br><a href=\"s\">Change...</a>");
		streamManager->updateStreamList();
	}
}

void URPPApplet::init() {
	playPauseButton = new PlayPauseButton();
	playPauseButton->setAttribute(Qt::WA_NoSystemBackground, true);
	playPauseButton->setEnabled(false);
	playPauseButtonProxy = new QGraphicsProxyWidget();
	playPauseButtonProxy->setWidget(playPauseButton);
	streamManager = new StreamManager(this);
	volumeProxy = new QGraphicsProxyWidget();
	volumeProxy->setWidget(streamManager->getVolumeWidget());
	volumeProxy->widget()->setAttribute(Qt::WA_NoSystemBackground, true);
	
	connect(streamManager, SIGNAL(newSong(QString, QString)), this, SLOT(onNewSong(QString, QString)));
	connect(streamManager, SIGNAL(buffering(int)), playPauseButton, SLOT(onBuffering(int)));
	connect(streamManager, SIGNAL(gotStream(bool)), playPauseButton, SLOT(setEnabled(bool)));
	connect(playPauseButton, SIGNAL(play()), streamManager, SLOT(play()));
	connect(playPauseButton, SIGNAL(pause()), streamManager, SLOT(pause()));

	QSettings settings;
	settingsLabel = new QLabel("<center>"+settings.value("stream", "128k MP3").toString()+"<br><a href=\"s\">Change...</a></center>");
	connect(settingsLabel, SIGNAL(linkActivated(QString)), this, SLOT(showSettings()));
	idleDetector = new IdleDetector(10*60, this); // stop if idle for more than 10 minutes
	connect(idleDetector, SIGNAL(idle()), this, SLOT(onUserIdle()));

	//connect(unattendedMode, SIGNAL(toggled(bool)), this, SLOT(onUnattendedModeToggled(bool)));
	leftContainer = new QWidget;
	QVBoxLayout* ll = new QVBoxLayout;
	ll->setContentsMargins(0,0,0,0);
	ll->addWidget(settingsLabel);
	ll->addWidget(idleDetector);
	leftContainer->setLayout(ll);
	leftContainer->setAttribute(Qt::WA_NoSystemBackground, true);
	leftProxy = new QGraphicsProxyWidget();
	leftProxy->setWidget(leftContainer);
	
	QGraphicsLinearLayout* hl = new QGraphicsLinearLayout();
	topBar = new QGraphicsWidget();
	topBar->setLayout(hl);
	scene()->addItem(topBar);
	hl->setContentsMargins(PADDING, PADDING, PADDING, PADDING);
	hl->addItem(leftProxy);
	hl->addStretch(50);
	hl->addItem(playPauseButtonProxy);
	hl->addStretch(50);
	hl->addItem(volumeProxy);
}

#ifdef PLASMA
void URPPApplet::resizeEvent(QGraphicsSceneResizeEvent* event) {
	Plasma::Applet::resizeEvent(event);
#else
void URPPApplet::resizeEvent(QResizeEvent* event) {
	scene()->setSceneRect(QRectF(0,0,event->size().width(),event->size().height()));
#endif
	if(topBar) {
		topBar->setGeometry(0,0,event->size().width(),50);
		if(centerItem) {
			centerItem->setPos(PADDING, topBar->size().height());
			centerItem->resize(QSize(topBar->size().width()-2*PADDING, qMin(200.0, (height()-topBar->size().height())/2)));
		}
	}
}
